﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.Serialization;
using GRV11;

namespace gpuimp.core
{
    [Serializable]
    public class Modifier
    {
        protected string fragPass1, fragPass2;
        protected string vertProg;

        private string name;

        

        protected int id;
        public int shaderPass1, shaderPass2;

        public bool twoPass;

        public bool isActive;

        public Modifier()
        {
            fragPass1 = null;
            fragPass2 = null;
            vertProg = null;

            id = -1;
            name = "";
            shaderPass1 = shaderPass2 = -1;
            isActive = true;
        }

        public void setupShader()
        {
            // Load shader
            if (true == CanvasManager.gr.bglCreateProgramObjectARB)
            {
                GRSupport.SupportShaderProgramCreate
                (
                    CanvasManager.gr,
                    vertProg,
                    fragPass1,
                    ref this.shaderPass1
                );

                GRSupport.SupportShaderProgramCreate
                (
                    CanvasManager.gr,
                    vertProg,
                    fragPass2,
                    ref this.shaderPass2
                );
            }
        }

        public void setCopyShader()
        {
            GRSupport.SupportShaderProgramSelect(CanvasManager.gr, Tool.copyShader);

            int shaderDiffuse = (int)CanvasManager.gr.glGetUniformLocationARB(Tool.copyShader, "diffuseMap");
            CanvasManager.gr.glUniform1iARB(shaderDiffuse, 0);
        }

        public void setupToolViewport(GR gr)
        {
            // Viewport 
            Point imageSize = CanvasManager.CurrentCanvas.ImageSize;
            
            // Viewport to same size as the texture
            gr.glViewport(0, 0, imageSize.X, imageSize.Y);

            // Clear the viewport
            gr.glClear(GR.GL_COLOR_BUFFER_BIT | GR.GL_DEPTH_BUFFER_BIT);

            // PROJECTION matrix
            gr.glMatrixMode(GR.GL_PROJECTION);
            gr.glLoadIdentity();

            int halfWidth = imageSize.X;
            int halfHeight = imageSize.Y;// *2;
            gr.gluOrtho2D(-imageSize.X, imageSize.X, -imageSize.Y, imageSize.Y);

            // MODELVIEW matrix, typically used to transform individual models
            gr.glMatrixMode(GR.GL_MODELVIEW);
            gr.glLoadIdentity();
        }

        public virtual void ActivatePass1()
        {
        }
        public virtual void ActivatePass2()
        {
        }

        public virtual void Deactivate()
        {
        }

        public virtual void setFragProg(String frag)
        {
        }


        public virtual void setVertProg(String vert)
        {
            //this.vertProg = vertProg;
            vertProg = @"varying vec2  texCoord;

void main(void)
{
   //gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );
   //gl_Position = sign( gl_Position );
    
   gl_Position = ftransform();
   texCoord = gl_MultiTexCoord0.xy;
}";
        }

        public virtual string getName()
        {
            return name;
        }
        protected string Name
        {
            get { return name; }
            set { name = value; }
        }
        public override string ToString()
        {
            return name;
        }
    }
}
